

//继承自EventBase的nativeManager类

import { AssetsMgr, resType } from "./assetsManager";
import DataMgr from "./dataManager";




export default class SoundMgr {
    private static instance: SoundMgr;

    effect: boolean = true;
    music: boolean = true;
    Audios: Map<string, Map<string, cc.AudioClip>> = new Map();
    private constructor() {



    }

    public static Ins() {
        if (!this.instance) {
            this.instance = new SoundMgr();
        }
        return this.instance;
    }

    //挂在节点上的Sound 调用他进行初始化

    async initAudioclip(bundleName: string, dirName: string) {

        const clipsArr: cc.Asset[] = await AssetsMgr.obj().preLoadRes(resType.dir, bundleName, dirName) as cc.Asset[];
        console.log(`initAudioclip bundleName:${bundleName} dirName:${dirName},clipsArr:${clipsArr.length}`)
        const map = this.Audios[bundleName];
        if (!map) {
            this.Audios[bundleName] = new Map();
        }


        clipsArr.forEach((clip) => {
            this.Audios[bundleName].set(clip.name, clip);
        })
    }

    initSound() {
        const effect = DataMgr.Ins().getLocalStorage('effect');
        const music = DataMgr.Ins().getLocalStorage('music');

        this.effect = !(effect == -1);
        this.music = !(music == -1);

    }

    setEffect(open: boolean) {
        this.effect = open;
    }

    setMusic(open: boolean) {
        this.music = open;
    }

    getClip(clipName: string) {
        // console.error('111111',clipName);
        for (let key in this.Audios) {
            const map = this.Audios[key];
            // console.error('222222',map);
            if (map.has(clipName)) {
                return map.get(clipName);
            }
        }
        return null;
    }
    playEffect(clipName: string, bundleName: string = null) {
        if (!this.effect) {
            return
        }
        const clip = this.getClip(clipName);
        if (!clip) {
            console.error('no such clip:', clipName);
            AssetsMgr.obj().loadRes(bundleName,  'music/'+clipName).then((asset: cc.AudioClip) => {
                cc.audioEngine.playEffect(asset, false);

                const map = this.Audios[bundleName];
                if (!map) {
                    this.Audios[bundleName] = new Map();
                }

                this.Audios[bundleName].set(asset.name, asset);

            }).catch(err => {
                console.error('load clip error:', err);
            });


            // console.log('this.Audios:',this.Audios)
            return;
        }
        console.log('playEffect:', clipName)
        cc.audioEngine.playEffect(clip, false);
    }

    playMusic(clipName: string, bundleName: string = null) {
        if (!this.music) {
            return
        }
        const clip = this.getClip(clipName);
        if (!clip) {
            console.error('no such clip:', clipName);

            AssetsMgr.obj().loadRes(bundleName, 'music/'+clipName).then((asset: cc.AudioClip) => {
                cc.audioEngine.playMusic(clip, true);

                const map = this.Audios[bundleName];
                if (!map) {
                    this.Audios[bundleName] = new Map();
                }

                this.Audios[bundleName].set(asset.name, asset);

            }).catch(err => {
                console.error('load clip error:', err);
            });



            return;
        }
        cc.audioEngine.playMusic(clip, true);
    }

    releaseAucioclip(mapKey: string) {
        if (!mapKey) {

            this.Audios.forEach((arr: Map<string, cc.AudioClip>, mapKey: string) => {
                this.Audios[mapKey].forEach((clip: cc.AudioClip, key: string) => {
                    cc.audioEngine.uncache(clip);
                    // AssetsMgr.obj().releaseRes(mapKey, key);
                });
            });

            this.Audios.clear();

            return;
        }
        if (!this.Audios[mapKey]) {
            // console.error('no such mapKey:',mapKey);
            return;
        }
        console.log('releaseAucioclip:', mapKey);
        this.Audios[mapKey].forEach((clip: cc.AudioClip, key: string) => {
            cc.audioEngine.uncache(clip);
            // AssetsMgr.obj().releaseRes(mapKey, key);
        });


        this.Audios[mapKey].clear();

    }

    pauseAllSound() {
        cc.audioEngine.pauseAll();
    }

    resumeAllSound() {
        cc.audioEngine.resumeAll();
    }


}